package org.weso.lodin.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.weso.lodin.to.DatasetTO;
import org.weso.lodin.to.ProviderTO;
import org.weso.lodin.utils.POIUtils;

public class IndicatorDAOImpl implements IndicatorDAO {

	public static final String SECONDARY_INDICATORS_SHEET_NAME = "SecondaryIndicatorsMetadata";//from RawData.xls
	public static final String SECONDARY = "Secondary";


	List<DatasetTO> indicatorTOs;
	
	public IndicatorDAOImpl(){
		this.indicatorTOs =  new LinkedList<DatasetTO>();
	}
	public IndicatorDAOImpl(InputStream data){
		this.indicatorTOs = load(data);
	}

	private List<DatasetTO> load(InputStream data) {
		List<DatasetTO> indicatorTOs = new LinkedList<DatasetTO>();
		try{
			HSSFWorkbook wb = new HSSFWorkbook(data);
			HSSFSheet sheet = wb.getSheet(SECONDARY_INDICATORS_SHEET_NAME);
			FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
			int rows = sheet.getPhysicalNumberOfRows();
			//FIXME: extract 2 to input parameter when the real data starts
			for (int r = 2; r < rows; r++) {
				HSSFRow row = sheet.getRow(r);
				if (row == null) {
					continue;
				}

				int cells = row.getPhysicalNumberOfCells();
				if(cells == 17 || cells == 18){
					DatasetTO currentIndicator = new DatasetTO();
					currentIndicator.setLayer(POIUtils.extractValue(row.getCell(0), evaluator));
					currentIndicator.setComponent(POIUtils.extractValue(row.getCell(1), evaluator));
					currentIndicator.setCode(POIUtils.extractValue(row.getCell(2), evaluator));
					currentIndicator.setIndicator((POIUtils.extractValue(row.getCell(3), evaluator)));
					currentIndicator.setVariableDescription(POIUtils.extractValue(row.getCell(4), evaluator));
					currentIndicator.setUriDataProvider(POIUtils.extractValue(row.getCell(6), evaluator));
					String []sources = POIUtils.extractValue(row.getCell(5), evaluator).split("/");
					List<ProviderTO> providers = new LinkedList<ProviderTO>();
					for(int i = 0; i<sources.length;i++){
						providers.add(
								new ProviderTO(
										POIUtils.extractValue(row.getCell(6), evaluator),
										sources[i]));	
					}
					currentIndicator.setSource(providers);									
					currentIndicator.setLatestReport((POIUtils.extractValue(row.getCell(7), evaluator)));
					for (int i = 8; i<11; i++){
						currentIndicator.getCriteria().add(POIUtils.extractValue(row.getCell(i), evaluator).equalsIgnoreCase("Yes"));							
					}
					currentIndicator.setCountryCoverage(POIUtils.extractValue(row.getCell(13), evaluator));
					currentIndicator.setTimeCoverage(POIUtils.extractValue(row.getCell(14), evaluator));
					currentIndicator.setDataRange(POIUtils.extractValue(row.getCell(15), evaluator));
					currentIndicator.setHigh(POIUtils.extractValue(row.getCell(16), evaluator).equalsIgnoreCase("High")); //Keep String?
					currentIndicator.setType(SECONDARY);
					indicatorTOs.add(currentIndicator);
				}
			
			}			
		}catch(IOException e){
			e.printStackTrace();
		}

		return indicatorTOs;
	}

		
	public List<DatasetTO> getSecondaryIndicators() {
		return this.indicatorTOs;
	}
	public List<DatasetTO> getPrimaryIndicators() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 
	CELL col=0 VALUE=STRING value=Layer
	CELL col=1 VALUE=STRING value=Component
	CELL col=2 VALUE=STRING value=Indicator Code
	CELL col=3 VALUE=STRING value=Indicator
	CELL col=4 VALUE=STRING value=Variable Description
	CELL col=5 VALUE=STRING value=Source
	CELL col=6 VALUE=STRING value=Data Provider Link
	CELL col=7 VALUE=STRING value=Latest Report
	CELL col=8 VALUE=STRING value=Criteria 1
	CELL col=9 VALUE=STRING value=Criteria 2
	CELL col=10 VALUE=STRING value=Criteria 3
	CELL col=11 VALUE=STRING value=Criteria 4
	CELL col=12 VALUE=STRING value=Criteria 5
	CELL col=13 VALUE=STRING value=Country Coverage
	CELL col=14 VALUE=STRING value=Time Coverage
	CELL col=15 VALUE=STRING value=Data Range
	CELL col=16 VALUE=STRING value=High/Low
	

 * 
	 */
	
}

